package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.ArticleDto;
import com.itheima.ydd.entity.Article;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.ArticleService;
import com.itheima.ydd.service.CateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 文章管理
 */
@Slf4j
@RestController
@RequestMapping("/ydd_article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CateService cateService;

    /**
     * 文章分页查询
     *
     * @param start_time
     * @param end_time
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Long start_time, Long end_time, String title, Integer cateid, Integer page, Integer limit) {

        //分页构造器
        Page<Article> pageInfo = new Page<>(page, limit);
        Page<ArticleDto> dtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //根据标题模糊查询
        queryWrapper.like(title != null, Article::getTitle, title);
        //根据分类id查询
        queryWrapper.eq(cateid != null, Article::getCateid, cateid);
        //在指定时间内修改的数据
        queryWrapper.between(start_time != null & end_time != null, Article::getCreate_date, start_time, end_time);
        //根据修改时间降序排列
        queryWrapper.orderByDesc(Article::getCreate_date);
        //分页查询
        articleService.page(pageInfo, queryWrapper);

        //拷贝
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<Article> articleList = pageInfo.getRecords();
        //遍历拷贝
        List<ArticleDto> articleDtoList = articleList.stream().map(item -> {
            ArticleDto articleDto = new ArticleDto();
            BeanUtils.copyProperties(item, articleDto);
            //根据id查询cate对象
            Cate cate = cateService.getById(item.getCateid());
            if (cate != null) {
                articleDto.setCatename(cate.getCatename());
            }
            return articleDto;
        }).collect(Collectors.toList());
        //数据放到dtopage中
        dtoPage.setRecords(articleDtoList);
        return ResultData.ok("", PageResult.init(dtoPage.getTotal(), dtoPage.getSize(), dtoPage.getCurrent(), dtoPage.getRecords()));
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(int id) {
        //根据id查询
        Article article = articleService.getById(id);
        if (article == null) {
            return ResultData.error("文章不存在");
        }
        //根据id删除
        articleService.removeById(id);
        return ResultData.ok("删除成功", null);
    }

    /**
     * 添加文章
     *
     * @param article
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Article article) {
        log.info(article.toString());
        //构造查询条件构造器
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Article::getTitle, article.getTitle());
        Article article1 = articleService.getOne(lqw);
        //判断文章是否存在
        if (article1 != null) {
            return ResultData.error("文章已存在");
        }
        //文章不存在 存入数据库
        article.setDefault_data("1");
        articleService.save(article);
        return ResultData.ok("添加成功", null);
    }

    /**
     * 修改数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id) {
        //根据id查询
        Article article = articleService.getById(id);
        return ResultData.ok("查询成功", article);
    }
}
